<template>
  <div class="flex flex-col h-full">
    <SearchContainer @search="getData" @reset="() => formRef.resetFields()">
      <a-form ref="formRef" class="form-style-flex" :model="formState">
        <a-form-item name="bqbh" label="" class="w-64">
          <SelectArea v-model:value="formState.bqid" use-cache set-cache @select="getData" />
        </a-form-item>
        <a-form-item name="cwh" label="" class="w-64 ml-2">
          <a-input v-model:value="formState.bedCode" placeholder="床位号" allow-clear />
        </a-form-item>
        <a-form-item name="zjm" label="" class="w-64 ml-2">
          <a-input v-model:value="formState.zjm" placeholder="姓名/助记码/住院号" allow-clear />
        </a-form-item>
      </a-form>
    </SearchContainer>
    <a-card class="mt-2 flex-1">
      <a-form ref="sFormRef" class="form-style-flex" :model="formState">
        <a-form-item name="mc" label="" class="w-64 -ml-1">
          <a-input v-model:value="formState.mc" placeholder="医嘱项目/助记码" @keyup.enter="getData" />
        </a-form-item>
        <a-form-item name="pyjm" label="" class="w-64 ml-2">
          <a-radio-group v-model:value="formState.inpAdvice" class="w-96 ml-4" @change="getData">
            <a-radio value="">全部</a-radio>
            <a-radio value="0">临时</a-radio>
            <a-radio value="1">长期</a-radio>
            <a-radio value="2">出院带药</a-radio>
          </a-radio-group>
        </a-form-item>
      </a-form>
      <div class="w-full flex justify-between">
        <BaseTable
          :loading="loading"
          column-code="00001032"
          class="w-2/3"
          row-key="inpAdviceid"
          :data-source="tableData"
          fill
          :pagination="pagination"
          :scroll="{ x: 2100 }"
          :custom-row="(record) => ({ onClick: () => onSelectRow(record) })"
          :row-class-name="(record) => (record.inpAdviceid === selectRow.inpAdviceid ? 'selected-row' : '')"
        >
          <template #bodyCell="{ column, record, index }">
            <template v-if="column.dataIndex === 'xh'">
              {{ index + 1 }}
            </template>
            <template v-if="column.dataIndex === 'advStatus'">
              <!-- 0：开立|1：已执行|2：作废|3：停止 -->
              <a-tag color="blue">{{ record.advStatusName || '-' }}</a-tag>
            </template>
            <template v-if="column.dataIndex === 'advExecType'">
              <!-- 医嘱执行类别;0：临时 1：长期 2：出院带药 -->
              <span v-if="record.advExecType == '0'">临时</span>
              <span v-if="record.advExecType == '1'">长期</span>
              <span v-if="record.advExecType == '2'">出院带药</span>
            </template>
            <template v-if="column.dataIndex === 'children'">
              <template v-if="record.advType === 0">
                <DoctorAdviceOrder
                  :row-data="record"
                  :params-key="{
                    mc: 'adv.mc',
                    gg: 'adv.gg',
                    sl: 'clinicalDose',
                    dw: 'clinicalDoseUnit',
                    yf: 'deliverRoute.fyff',
                    pl: 'fqy.plid',
                    drugPathName: 'drugPathName'
                  }"
                />
              </template>
              <template v-else>
                <div class="pl-2">
                  {{ record?.advName }}
                </div>
              </template>
            </template>
            <template v-if="column.dataIndex === 'action'">
              <a-button v-if="record.inpAdviceExecNum === 0" type="link" size="small" @click="cancelSignApply(record, 0)">撤销签收</a-button>
              <span v-else>-</span>
            </template>
          </template>
        </BaseTable>
        <BaseTable
          :loading="exLoading"
          column-code="000740"
          class="w-1/3 ml-2"
          :data-source="dataResource"
          fill
          :pagination="pagination1"
          :scroll="{ x: 1200 }"
        >
          <template #bodyCell="{ column, record, index }">
            <template v-if="column.dataIndex === 'xh'">
              {{ index + 1 }}
            </template>
            <template v-if="column.dataIndex === 'execNurseStatus'">
              <!-- execStatus：执行状态  0：待执行(非药品)在此改为已申请 1：已执行 2：待执行（已发药）如果为非要再次显示已申请3：已退药 4：退药中   -->
              <a-tag v-if="record.execStatus === 0" color="blue">已申请</a-tag>
              <a-tag v-if="record.execStatus === 2" color="blue">{{ isDrug(record.advTypeCode) ? '已发药' : '已申请' }}</a-tag>
              <a-tag v-if="record.execStatus === 1" color="success">已执行</a-tag>
              <a-tag v-if="record.execStatus === 3" color="green">已退药</a-tag>
              <a-tag v-if="record.execStatus === 4" color="red">退药中</a-tag>
            </template>
            <template v-if="column.dataIndex === 'children'">
              <DoctorAdviceOrder
                :row-data="record"
                :params-key="{
                  mc: 'adv.mc',
                  gg: 'adv.gg',
                  sl: 'clinicalDose',
                  dw: 'adv.jldw',
                  yf: 'deliverRoute.fyff',
                  pl: 'fqy.pl',
                  drugPathName: 'drugPathName'
                }"
              />
            </template>
            <template v-if="column.dataIndex === 'action'">
              <a-button
                v-if="record.execStatus === 0 || record.execStatus === 3 || (record.execStatus === 2 && !isDrug(record.advTypeCode))"
                type="link"
                size="small"
                @click="cancelSignApply(record, 1)"
                >撤销申请</a-button
              >
              <span v-else>-</span>
              <a-button type="link" size="small" @click="onClcikTime(record)">调整执行</a-button>
            </template>
          </template>
        </BaseTable>
      </div>
    </a-card>
    <CancelModal v-model:open="cancelOpen" :row-data="rowData" :cancel-type="cancelType" :allow-cancel-advice="allowCancelAdvice" @success="onSuccess" />
    <AdjustModal v-model:open="adjustOpen" :row-data="rowData" @success="getExecuteData" />
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import SearchContainer from '@/components/SearchContainer'
import BaseTable from '@/components/BaseTable'
import SelectArea from '@/components/SelectArea'
import CancelModal from './components/CancelModal.vue'
import AdjustModal from './components/AdjustModal.vue'

const formRef = ref(null)
const formState = ref({
  inpAdvice: ''
})
const selectRow = ref({})
const cancelOpen = ref(false)
const adjustOpen = ref(false)
const cancelType = ref(0) //0 撤销签收 1 撤销申请
const rowData = ref({})
const allowCancelAdvice = ref(false)
const { loading, tableData, pagination, getData } = useTable({
  url: '/inp/clinicalOrder/getClinicalOrderAllList',
  immediate: true,
  params: {},
  formatParams: () => {
    return {
      ...formState.value
    }
  }
  // formatResult: (result) => {
  //   result.forEach((i) => {
  //     i.advOrderType = 0
  //   })
  //   return result
  // }
})
const onSelectRow = (select) => {
  selectRow.value = select
  getExecuteData()
}
const {
  tableData: dataResource,
  getData: getExecuteData,
  pagination: pagination1,
  loading: exLoading
} = useTable({
  url: '/inp/clinicalOrder/getClinicalOrderExecuteList',
  formatParams: () => {
    return {
      inpAdviceId: selectRow.value.inpAdviceid,
      showAllAdvice: true,
      yzglFlag: true
    }
  }
})
const cancelSignApply = (record, type) => {
  rowData.value = record
  cancelType.value = type
  if (type === 0) {
    allowCancelAdvice.value = record.allowCancelSignedInpAdvice
  } else {
    allowCancelAdvice.value = record.allowCancelApplyInpAdvice
  }
  cancelOpen.value = true
}

// 调整执行时间
const onClcikTime = (record) => {
  adjustOpen.value = true
  rowData.value = { ...record }
}

const onSuccess = () => {
  if (cancelType.value === 0) {
    getData()
  } else {
    getData()
    getExecuteData()
  }
}
const isDrug = (code) => {
  // advTypeCode  2,4,5为药品别的为非药品
  if (code === 2 || code === 4 || code === 5) {
    return true
  } else {
    return false
  }
}

onMounted(() => {
  // queryList()
})
</script>
<style lang="less" scoped>
:deep(.ant-table-row-expand-icon) {
  display: none;
}
</style>
